package com.juidemo.algoframe;

import com.juidemo.tools.AlgoVisHelper;
import javax.swing.*;
import java.awt.*;

public class AlgoFrame extends JFrame {
    private int canvasWidth;
    private int canvasHeight;

    public AlgoFrame(String title, int canvasWidth, int canvasHeight) throws HeadlessException {
        super(title);
        this.canvasWidth = canvasWidth;
        this.canvasHeight = canvasHeight;

        //画布相关
        AlgoCanvas canvas = new AlgoCanvas();
//        canvas.setPreferredSize(new Dimension(canvasWidth, canvasHeight));
        this.setContentPane(canvas);
        //跟进加载进内容的大小，调整窗口的大小
        this.pack();
        //窗口大小 删除这行代码，使用上面画布的大小
//        this.setSize(canvasWidth, canvasHeight);
        //是否可改变窗口大小
        this.setResizable(false);
        //关闭的时候，传递的功能
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //显示jframe窗口
        this.setVisible(true);
    }

    public AlgoFrame(String title) throws HeadlessException {
        this(title, 1024, 768);
    }

    public int getCanvasWidth() {
        return canvasWidth;
    }

    public int getCanvasHeight() {
        return canvasHeight;
    }

    private class AlgoCanvas extends JPanel {

        public AlgoCanvas() {
            super(true);
        }

        //绘制组件
        @Override
        public void paintComponent(Graphics g){
            super.paintComponent(g);
//            g.drawOval(50,50,300,300);
            //Graphics2D绘制
            Graphics2D g2d = (Graphics2D) g;

            //抗锯齿
            RenderingHints hints = new RenderingHints(
                                            RenderingHints.KEY_ANTIALIASING,
                                            RenderingHints.VALUE_ANTIALIAS_ON);
            g2d.addRenderingHints(hints);

            //设置边框宽度
//            int strokeWidth = 5;
//            g2d.setStroke(new BasicStroke(strokeWidth));
            AlgoVisHelper.setStrokeWidth(g2d, 5);

            //基本图形封装在Ellipse2D对象中

            //设置颜色
//            g2d.setColor(Color.BLUE);
            AlgoVisHelper.setColor(g2d, Color.BLUE);
//            Ellipse2D circle2 = new Ellipse2D.Double(50,50,300,300);
//            //绘制实心圆
//            g2d.fill(circle2);
            //实心圆
            AlgoVisHelper.fillCircle(g2d, canvasWidth/2, canvasHeight/2, 200);

            //设置颜色
//            g2d.setColor(Color.RED);
            AlgoVisHelper.setColor(g2d, Color.RED);
            //图形坐标 左上角坐标，长和宽
//            Ellipse2D circle = new Ellipse2D.Double(50,50,300,300);
//            //绘制空心圆
//            g2d.draw(circle);
            AlgoVisHelper.strokeCircle(g2d, canvasWidth/2, canvasHeight/2, 200);


        }

        //代替第17行代码
        // canvas.setPreferredSize(new Dimension(canvasWidth, canvasHeight));
        //根据面板自动调整窗口大小
        @Override
        public Dimension getPreferredSize(){
            return new Dimension(canvasWidth, canvasHeight);
        }
    }

}
